using System;
using System.Windows.Forms;

namespace demo
{
    /// <summary>
    /// WallpaperSlideshow 使用示例
    /// 演示如何在 Windows Forms 应用程序中使用 WallpaperSlideshow 类
    /// </summary>
    public partial class WallpaperSlideshowUsageExample : Form
    {
        private WallpaperSlideshow wallpaperSlideshow;
        private Button btnStart;
        private Button btnStop;
        private Button btnNext;
        private Button btnPrevious;
        private Label lblStatus;
        private Label lblCurrentImage;
        private NumericUpDown nudInterval;
        private Label lblInterval;

        public WallpaperSlideshowUsageExample()
        {
            InitializeComponent();
            InitializeWallpaperSlideshow();
        }

        private void InitializeComponent()
        {
            this.btnStart = new Button();
            this.btnStop = new Button();
            this.btnNext = new Button();
            this.btnPrevious = new Button();
            this.lblStatus = new Label();
            this.lblCurrentImage = new Label();
            this.nudInterval = new NumericUpDown();
            this.lblInterval = new Label();
            ((System.ComponentModel.ISupportInitialize)(this.nudInterval)).BeginInit();
            this.SuspendLayout();

            // btnStart
            this.btnStart.Location = new System.Drawing.Point(12, 12);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new System.Drawing.Size(75, 23);
            this.btnStart.Text = "开始";
            this.btnStart.UseVisualStyleBackColor = true;
            this.btnStart.Click += new EventHandler(this.btnStart_Click);

            // btnStop
            this.btnStop.Location = new System.Drawing.Point(93, 12);
            this.btnStop.Name = "btnStop";
            this.btnStop.Size = new System.Drawing.Size(75, 23);
            this.btnStop.Text = "停止";
            this.btnStop.UseVisualStyleBackColor = true;
            this.btnStop.Click += new EventHandler(this.btnStop_Click);

            // btnNext
            this.btnNext.Location = new System.Drawing.Point(174, 12);
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(75, 23);
            this.btnNext.Text = "下一张";
            this.btnNext.UseVisualStyleBackColor = true;
            this.btnNext.Click += new EventHandler(this.btnNext_Click);

            // btnPrevious
            this.btnPrevious.Location = new System.Drawing.Point(255, 12);
            this.btnPrevious.Name = "btnPrevious";
            this.btnPrevious.Size = new System.Drawing.Size(75, 23);
            this.btnPrevious.Text = "上一张";
            this.btnPrevious.UseVisualStyleBackColor = true;
            this.btnPrevious.Click += new EventHandler(this.btnPrevious_Click);

            // lblInterval
            this.lblInterval.AutoSize = true;
            this.lblInterval.Location = new System.Drawing.Point(12, 50);
            this.lblInterval.Name = "lblInterval";
            this.lblInterval.Size = new System.Drawing.Size(89, 12);
            this.lblInterval.Text = "切换间隔(分钟):";

            // nudInterval
            this.nudInterval.Location = new System.Drawing.Point(107, 48);
            this.nudInterval.Maximum = new decimal(new int[] { 60, 0, 0, 0 });
            this.nudInterval.Minimum = new decimal(new int[] { 1, 0, 0, 0 });
            this.nudInterval.Name = "nudInterval";
            this.nudInterval.Size = new System.Drawing.Size(120, 21);
            this.nudInterval.Value = new decimal(new int[] { 10, 0, 0, 0 });
            this.nudInterval.ValueChanged += new EventHandler(this.nudInterval_ValueChanged);

            // lblStatus
            this.lblStatus.AutoSize = true;
            this.lblStatus.Location = new System.Drawing.Point(12, 80);
            this.lblStatus.Name = "lblStatus";
            this.lblStatus.Size = new System.Drawing.Size(41, 12);
            this.lblStatus.Text = "状态: ";

            // lblCurrentImage
            this.lblCurrentImage.AutoSize = true;
            this.lblCurrentImage.Location = new System.Drawing.Point(12, 110);
            this.lblCurrentImage.Name = "lblCurrentImage";
            this.lblCurrentImage.Size = new System.Drawing.Size(65, 12);
            this.lblCurrentImage.Text = "当前图片: ";

            // Form
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(400, 200);
            this.Controls.Add(this.lblCurrentImage);
            this.Controls.Add(this.lblStatus);
            this.Controls.Add(this.nudInterval);
            this.Controls.Add(this.lblInterval);
            this.Controls.Add(this.btnPrevious);
            this.Controls.Add(this.btnNext);
            this.Controls.Add(this.btnStop);
            this.Controls.Add(this.btnStart);
            this.Name = "WallpaperSlideshowUsageExample";
            this.Text = "壁纸切换示例";
            ((System.ComponentModel.ISupportInitialize)(this.nudInterval)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void InitializeWallpaperSlideshow()
        {
            // 初始化壁纸切换器
            wallpaperSlideshow = new WallpaperSlideshow(this);

            // 订阅事件
            wallpaperSlideshow.WallpaperChanged += OnWallpaperChanged;
            wallpaperSlideshow.ErrorOccurred += OnErrorOccurred;

            // 更新UI状态
            UpdateUI();
        }

        private void OnWallpaperChanged(object sender, WallpaperChangedEventArgs e)
        {
            // 在UI线程上更新界面
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => OnWallpaperChanged(sender, e)));
                return;
            }

            lblCurrentImage.Text = $"当前图片: {System.IO.Path.GetFileName(e.ImagePath)}";
        }

        private void OnErrorOccurred(object sender, WallpaperErrorEventArgs e)
        {
            // 在UI线程上显示错误
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => OnErrorOccurred(sender, e)));
                return;
            }

            MessageBox.Show(e.ErrorMessage, "壁纸切换错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            var result = wallpaperSlideshow.Start();
            if (!result.Success)
            {
                MessageBox.Show(result.ErrorMessage, "启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            UpdateUI();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            wallpaperSlideshow.Stop();
            UpdateUI();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            var result = wallpaperSlideshow.NextWallpaper();
            if (!result.Success)
            {
                MessageBox.Show(result.ErrorMessage, "切换失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            var result = wallpaperSlideshow.PreviousWallpaper();
            if (!result.Success)
            {
                MessageBox.Show(result.ErrorMessage, "切换失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void nudInterval_ValueChanged(object sender, EventArgs e)
        {
            // 设置新的间隔时间（转换为毫秒）
            wallpaperSlideshow.IntervalMinutes = (int)nudInterval.Value;
        }

        private void UpdateUI()
        {
            bool isRunning = wallpaperSlideshow.IsRunning;
            btnStart.Enabled = !isRunning;
            btnStop.Enabled = isRunning;
            lblStatus.Text = $"状态: {(isRunning ? "运行中" : "已停止")} (共{wallpaperSlideshow.ImageCount}张图片)";
            
            if (!string.IsNullOrEmpty(wallpaperSlideshow.CurrentImagePath))
            {
                lblCurrentImage.Text = $"当前图片: {System.IO.Path.GetFileName(wallpaperSlideshow.CurrentImagePath)}";
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            // 清理资源
            wallpaperSlideshow?.Dispose();
            base.OnFormClosing(e);
        }
    }
}